﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.IO;

namespace S7.Windows.Music
{
    /// <summary>
    /// Interaction logic for AddMusic.xaml
    /// </summary>
    public partial class AddMusic : Window
    {
        public AddMusic()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            var genres = from genre in Genre.lstGenre
                         where genre.GenreName.Equals(cbGenres.SelectedValue.ToString())
                         select genre;
            int GenreID = genres.ToList()[0].GenreID;

            var artists = from artist in Artist.lstArtists
                          where artist.ArtistName.Equals(tbArtist.Text)
                          select artist;
            int? ArtistID;

            if (artists.ToList().Count.Equals(0))
            {
                Artist artist = new Artist();
                artist.ArtistName = tbArtist.Text;
                ArtistID = artist.Add();
            }
            else
            {
                ArtistID = artists.ToList()[0].ArtistID;
            }


            Song song = new Song();
            // song.Name =
            song.ArtistID = ArtistID;
            song.GenreID = GenreID;
            song.SongPath = tbPath.Text;

            song.Add();

            MessageBoxResult result = MessageBox.Show("Move to Library? \n Select Yes to move \n No to Copy and keep the original file", "Add Music", MessageBoxButton.YesNo, MessageBoxImage.None, MessageBoxResult.Yes);
            if (result == MessageBoxResult.Yes)
            {
                File.Move(tbPath.Text, Properties.Settings.Default.MediaPath + "//" + cbGenres.SelectedValue.ToString() + "//" + tbArtist.Name + "//" + ".mp3");
            }
            else
            {
                File.Copy(tbPath.Text, Properties.Settings.Default.MediaPath + "//" + cbGenres.SelectedValue.ToString() + "//" + tbArtist.Name + "//" + ".mp3");
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openFile = new Microsoft.Win32.OpenFileDialog();
            openFile.Multiselect = false;
            bool result = openFile.ShowDialog().Value;
            if (result)
            {
                tbPath.Dispatcher.Invoke(new ThreadStart(() =>
                    {
                        tbPath.Text = openFile.FileName;
                    }
                ));
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Thread thr = new Thread(new ThreadStart(() =>
                {
                    GetGenres();
                    GetArtists();
                }));
            thr.IsBackground = true;
            thr.Start();
        }

        private void btnAddGenre_Click(object sender, RoutedEventArgs e)
        {
            AddGenre window = new AddGenre();
            bool? result =  window.ShowDialog();

            if (result.Value.Equals(true))
            {
                Thread thr = new Thread(new ThreadStart(() =>
                {
                    GetGenres();
                }));
                thr.IsBackground = true;
                thr.Start();
            }
        }

        private void GetGenres()
        {
            Genre.lstGenre = Genre.Search();
            cbGenres.Dispatcher.Invoke(new ThreadStart(() =>
            {
                try
                {
                    foreach (var genre in Genre.lstGenre)
                    {
                        cbGenres.Items.Add(genre.GenreName);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                }
            }));
        }

        private void GetArtists()
        {
            Artist.lstArtists = Artist.Search();
        }
    }
}
